// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

#pragma once

#include <spdlog/details/file_helper.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/sinks/base_sink.h>

#include "../archiver/archiver.h"

#include <mutex>
#include <string>

namespace spdlog {
    using namespace archive;
namespace sinks {

//
// Rotating file sink based on size
//
template <typename Mutex, typename ArchivePtrType>
class backup_rotating_file_sink final : public base_sink<Mutex> {
public:
    static constexpr size_t MaxFiles = 200000;

    backup_rotating_file_sink(filename_t base_filename,
                              std::size_t max_size,
                              std::size_t max_files,
                              ArchivePtrType parchiver,
                              bool rotate_on_open,
                              const file_event_handlers& event_handlers);
    virtual ~backup_rotating_file_sink();
    filename_t calc_filename(const filename_t& filename, std::size_t index);
    filename_t filename();
    void rotate_now();
    void set_max_size(std::size_t max_size);
    std::size_t get_max_size();
    void set_max_files(std::size_t max_files);
    std::size_t get_max_files();

protected:
    void sink_it_(const details::log_msg& msg) override;
    void flush_() override;

private:
    // Rotate files:
    // log.txt -> log.1.txt
    // log.1.txt -> log.2.txt
    // log.2.txt -> log.3.txt
    // log.3.txt -> backup to log.zip -> delete log.3.txt
    void rotate_();

    // delete the target if exists, and rename the src file to target
    // return true on success, false otherwise.
    bool rename_file_(const filename_t& src_filename, const filename_t& target_filename, const std::size_t index);

    void backup_file(const filename_t& target_filename);

    filename_t base_filename_;
    std::size_t max_size_;
    std::size_t max_files_;
    ArchivePtrType parchiver_;
    std::size_t current_size_;
    details::file_helper file_helper_;

    spdlog::filename_t dir_filename;
    spdlog::filename_t ext;
    Mutex backup_mutex_;
};


using backup_rotating_file_sink_mt = backup_rotating_file_sink<std::mutex, std::shared_ptr<archiver>>;
using backup_rotating_file_sink_st = backup_rotating_file_sink<details::null_mutex, std::shared_ptr<archiver>>;


}  // namespace sinks

//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory, typename T>
std::shared_ptr<logger> backup_rotating_logger_mt(const std::string& logger_name,
                                                  const filename_t& filename,
                                                  size_t max_file_size,
                                                  size_t max_files,
                                                  T&& parchiver,
                                                  bool rotate_on_open = false,
                                                  const file_event_handlers& event_handlers = {}){
    return Factory::template create<sinks::backup_rotating_file_sink<std::mutex, std::decay_t<T>>>(
        logger_name, 
        filename, 
        max_file_size, 
        max_files,
        std::forward<T>(parchiver),
        rotate_on_open,
        event_handlers);
}

template <typename Factory = spdlog::synchronous_factory, typename T>
std::shared_ptr<logger> backup_rotating_logger_st(const std::string& logger_name,
                                                  const filename_t& filename,
                                                  size_t max_file_size,
                                                  size_t max_files,
                                                  T&& parchiver,
                                                  bool rotate_on_open = false,
                                                  const file_event_handlers& event_handlers = {}) {
    return Factory::template create<sinks::backup_rotating_file_sink<std::mutex, std::decay_t<T>>>(
        logger_name, 
        filename, 
        max_file_size, 
        max_files, 
        std::forward<T>(parchiver),
        rotate_on_open,
        event_handlers);
}
}  // namespace spdlog

#ifdef SPDLOG_HEADER_ONLY
    #include "backup_rotating_file_sink-inl.h"
#endif
